﻿using JustMetClient.Commons;
using JustMetClient.Models.InfoModels;
using JustMetClient.Models.WebApiModels;
using JustMetClient.Modules.LoginModule;
using JustMetClient.Modules.WebModule;
using Azylee.Core.DataUtils.CollectionUtils;
using Azylee.Core.DataUtils.UnitConvertUtils;
using Azylee.Core.IOUtils.DirUtils;
using Azylee.Core.IOUtils.FileUtils;
using Azylee.Core.IOUtils.TxtUtils;
using Azylee.Core.NetUtils;
using Azylee.Core.Plus.DataUtils.JsonUtils;
using Azylee.YeahWeb.BaiDuWebAPI.IPLocationAPI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JustMetClient.Models.WarningModels;

namespace JustMetClient.Modules.PlanTaskModule
{
    public class PlanTaskM10
    {
        /// <summary>
        /// 10分钟任务
        /// </summary>
        public static void Do()
        {
            //SocketSendQueue.AddToQueue("37", "37", 60); //读取网络监测配置
            //SocketSendQueue.AddToQueue("35", ip, 60); // 发送USB白名单版本请求
            //BaseTools.ClearMemory();

            #region 定时删除临时文件
            List<string> temp_files = FileTool.GetFile(R.Paths.Temp);
            if (ListTool.HasElements(temp_files))
            {
                foreach (var file in temp_files)
                {
                    try
                    {
                        FileInfo file_info = new FileInfo(file);
                        if (file_info.LastWriteTime.AddMinutes(30) < DateTime.Now) FileTool.Delete(file);
                    }
                    catch { }
                }
            }
            #endregion



            #region 超过10分钟Socket未通信（接收、发送），并且Socket状态为已连接，则报警疑似Socket失联
            if (R.Socket.LastReadTime < DateTime.Now.AddMinutes(-10) || R.Socket.LastSendTime < DateTime.Now.AddMinutes(-10))
            {
                if (R.Socket.IsConnect)
                {
                    WebApiSendQueue.Add(R.Web.Send.ClientCollection, new ClientUserDefinedInfoModel()
                    {
                        Type = "Socket已10分钟未通信",
                        Values = ArrayTool.Formatter(new string[] { R.Socket.LastReadTime.ToString(), R.Socket.LastSendTime.ToString() }, "", 10),
                    }, HttpMethodType.Post, 5);
                }
            }
            #endregion

            #region 获取IP地址定位信息
            var loc = IPLocationTool.GetLocation();
            if (loc != null && !loc.Like(R.IPLocation))
            {
                R.IPLocation = loc;
                R.Log.i("::IPLocation::" + JsonTool.ToStr(R.IPLocation));
            }
            #endregion

            #region 检测外网是否连接（Ping 阿里DNS和百度DNS）
            if (PingTool.Ping("223.5.5.5") && PingTool.Ping("180.76.76.76"))
            {
                //发送到外网服务器数据
                WebApiSendQueue.Add(R.Web.Send.InternetWarning, new InternetWarningModel()
                {
                    Name = R.UserName,
                    Location = R.IPLocation.ToString(),
                    LastAccount = IniTool.GetString(R.Files.Settings, "User", "LastAccount", ""),
                    LastName = IniTool.GetString(R.Files.Settings, "User", "LastName", ""),
                }, HttpMethodType.Post);

                ////调用802断开连接
                //NET802Helper.Disconnect(out string code, out string desc);
            }
            #endregion
        }
    }
}
